{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unexpected indent (<ipython-input-7-3c057172bc58>, line 19)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-7-3c057172bc58>\"\u001b[1;36m, line \u001b[1;32m19\u001b[0m\n\u001b[1;33m    print(\"'周瑜'的词向量为:\\n\",model.wv.get_vector('周瑜'))\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n"
     ]
    }
   ],
   "source": [
    "import jieba\n",
    "import re\n",
    "from gensim.models import Word2Vec\n",
    "\n",
    "with open(r\"data/sanguo.txt\",enoding='utf-8')as f:\n",
    "    lines=[]\n",
    "    for line in f:\n",
    "        temp=jieba.lcut(line)\n",
    "        words=[]\n",
    "        for i in temp:\n",
    "            i = re.sub(\"[\\s+\\.\\!\\/_,$%^*(+\\\"\\'””《》]+|[+——！，。？、~@#￥%……&*（）：；‘]+\", \"\", i)\n",
    "          \n",
    "        if len(i)>0:\n",
    "            words.append(i)\n",
    "    if len(words)>0:\n",
    "        lines.append(words)\n",
    "model=Word2Vec(lines,vector_size=20,window=2,min_count=3,\n",
    "epochs=7,negative=10,sg=1)\n",
    "    print(\"'周瑜'的词向量为:\\n\",model.wv.get_vector('周瑜'))\n",
    "    print(\"与'周瑜'相似度最高的10个词:\")\n",
    "    print(model.wv.most_similar('周瑜',topn=10))\n",
    "    print(\"'刘备'和'曹操'的相似度:{}\".format(model.wv.similarity('刘备','曹操')))\n",
    "    words=\"孙权 曹操 刘备 孙夫人\"\n",
    "    print(\"'在词/孙权/曹操/刘备/孙夫人'中,'{}'与其他词不属于同一类\".format(model.wv.doesnt_match(words.split())))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik,9ol.0p;/"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
